/*
 * @Description: 编辑弹窗配置（包含新增编辑）
 * @Author: HuiSir<https://gitee.com/huisir001>
 * @Date: 2021-11-05 11:41:30
 * @LastEditTime: 2025-08-16 17:50:06
 */
import { defineFormOptions } from "@/components/TablePage/predefined";
import { PermTypeOpt, MenuTypeOpt } from "@/helpers/selection";

export default defineFormOptions(() => [
  {
    label: "上级权限",
    key: "pid",
    slot: "parent",
    default: null,
  },
  {
    label: "权限名称",
    component: "input",
    key: "title",
    props: {
      placeholder: "请输入权限名称",
    },
    rules: {
      required: true,
    },
  },
  {
    label: "权限编码",
    key: "code",
    component: "input",
    props: {
      placeholder: "输入唯一编码(英文字符)，避免重复",
    },
    rules: [
      {
        required: true,
      },
      {
        pattern: /^[a-zA-z_-]+$/,
        trigger: "blur",
        message: "编码须为英文及“_”或“-”组成",
      },
    ],
  },
  {
    label: "排序",
    component: "input-number",
    key: "orderRank",
    props: {
      controlsPosition: "right",
    },
  },
  {
    label: "权限类型",
    key: "type",
    component: "select",
    options: PermTypeOpt.options,
    rules: {
      required: true,
    },
  },
  {
    label: "菜单类型",
    key: "menuType",
    component: "select",
    options: MenuTypeOpt.options,
    default: MenuTypeOpt.route,
    hide(formData) {
      return formData.type !== PermTypeOpt.menu;
    },
    rules: {
      required: true,
    },
  },
  {
    label: "图标",
    slot: "icon",
    key: "icon",
    hide(formData) {
      return formData.type !== PermTypeOpt.menu;
    },
  },
  {
    label: "路径",
    component: "input",
    key: "path",
    desc(formData) {
      return formData.menuType === MenuTypeOpt.route ||
        formData.type === PermTypeOpt.page
        ? "输入访问路径如`/home`"
        : "输入外链如`http://abc.com`";
    },
    props: {
      placeholder: "请输入路径",
    },
    rules: {
      required: true,
    },
    hide(formData) {
      return permNotRouteOrLink(formData as Permission);
    },
  },
  {
    label: "路由文件",
    key: "viewPath",
    slot: "viewPath",
    rules: {
      required: true,
    },
    hide(formData) {
      return permNotRoute(formData as Permission);
    },
  },
  {
    label: "需要登录",
    component: "switch",
    key: "internal",
    props: {
      activeText: "需要",
      inactiveText: "无需",
    },
    desc: "是否需要登录才能访问",
    hide(formData) {
      return permNotRoute(formData as Permission);
    },
  },
  {
    label: "路由缓存",
    component: "switch",
    key: "keepAlive",
    props: {
      activeText: "缓存",
      inactiveText: "不缓存",
    },
    desc: "路由是否缓存，缓存时切换已有路由不刷新",
    hide(formData) {
      return permNotRoute(formData as Permission);
    },
  },
  {
    label: "打开方式",
    component: "switch",
    key: "blank",
    props: {
      activeText: "浏览器新标签",
      inactiveText: "框架内",
    },
    hide(formData) {
      return permNotRouteOrLink(formData as Permission);
    },
  },
  {
    label: "事件名",
    component: "input",
    key: "triggerMethod",
    props: {
      placeholder: "方法名(store->common模块)",
    },
    rules: [
      {
        required: true,
      },
      {
        trigger: "blur",
        pattern: /^[A-Za-z]+(\w+)*$/,
        message: "由英文、数字、下划线组成，以英文开头，如“login”",
      },
    ],
    hide(formData) {
      return (
        formData.type !== PermTypeOpt.menu ||
        formData.menuType !== MenuTypeOpt.button
      );
    },
  },
]);

/**
 * 判断权限非路由或链接
 */
function permNotRouteOrLink(formData: Permission) {
  return (
    formData.type === PermTypeOpt.func ||
    (formData.type === PermTypeOpt.menu &&
      [MenuTypeOpt.button, MenuTypeOpt.category].includes(
        formData.menuType as MenuType
      ))
  );
}

/**
 * 判断权限非路由
 */
function permNotRoute(formData: Permission) {
  return (
    formData.type !== PermTypeOpt.page &&
    formData.menuType !== MenuTypeOpt.route
  );
}
